Repository navigation
Conversation
TypeAdapterRuntimeTypeWrapper.write resolved the runtime type adapter unconditionally, so when only a reflective adapter could be created for the runtime type and its creation failed (for example a JDK-internal class in a package java.base does not open), the failure escaped to the caller even though the wrapper would have discarded that adapter and used the adapter for the declared type. The lookup now goes through the new internal Gson.getOptionallyReflectiveAdapter (exposed like JsonReaderInternalAccess via GsonInternalAccess), which walks the factories up to the reflective one: adapters selected before it are returned with the usual caching, and if only the reflective factory could handle the type it returns null without creating the adapter, unless allowReflective is set, in which case behavior is unchanged. Failures of user-registered factories for the runtime type keep propagating, since that adapter is the first preference.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3122
Purpose
When a value is serialized as an element of an array or a collection (or through any other
TypeAdapterRuntimeTypeWrapperpath), the adapter for the runtime type was resolved unconditionally viaGson.getAdapter. When only a reflective adapter could be created for the runtime type and its creation failed — for example forsun.reflect.generics.reflectiveObjects.WildcardTypeImpl, whose packagejava.basedoes not export — thatJsonIOExceptionescaped to the caller even in the branch where the wrapper discards the runtime adapter and uses the adapter for the declared type. Registering an adapter for the declared interface was therefore enough to serialize a value directly, but not to serialize the same value inside an array or a collection.Description
Implements the direction proposed by @eamonnmcmanus in this comment: a structural lookup
following the sketch by @Marcono1234. The lookup walks the factories in order:
ReflectiveTypeAdapterFactoryrun normally: their adapters are returned and cached likegetAdapterdoes (typeTokenCacheplus theFutureTypeAdapterrecursion guard), and failures of user-registered factories for the runtime type keep propagating untouched, since such an adapter would be the first preference and its error must not be silenced.allowReflective == falsethe lookup stops and returnsnullwithout ever callingReflectiveTypeAdapterFactory.create, and the wrapper keeps the delegate for the declared type. WithallowReflective == true(the delegate itself is reflective) the reflective adapter is created as before and its creation failures keep failing fast.The method is exposed to
com.google.gson.internal.bindthrough a newGsonInternalAccessbridge modeled afterJsonReaderInternalAccess, so it stays out of the public API.This supersedes #3123 by @jaideeppyne, which caught
JsonIOExceptionaround the lookup instead; the discussion on the issue established that catching cannot distinguish the reflective factory's own failure from a failure of a user factory that should have won.What does not change
ReflectiveTypeAdapterFactory.Adapteronly, per Fix TypeAdapterRuntimeTypeWrapper not detecting reflective TreeTypeAdapter and FutureTypeAdapter #1787.readstill delegates unchanged.Both are covered by
testUserFactoryFailureForRuntimeTypePropagatesandtestReflectiveCreationFailureStillPropagatesWhenDelegateIsReflectiveinTypeAdapterRuntimeTypeWrapperTest.Verification
mvn -pl :gson test -Dtest=TypeAdapterRuntimeTypeWrapperTest— Tests run: 11, Errors: 2 (testCustomAdapterForDeclaredTypeUsedForArrayElementsandtestCustomAdapterForDeclaredTypeUsedForCollectionElements, both failing withJsonIOException: Failed making field 'sun.reflect.generics.reflectiveObjects.WildcardTypeImpl#upperBounds' accessible).gsonmodulemvn -pl :gson verify(includes the spotless check): Tests run: 4761, Failures: 0, Errors: 0, Skipped: 23 (pre-existing skips). Temurin 21.